package com.trivia.fortune.sdk;

import android.util.Log;

import com.trivia.fortune.sdk.ad.TrFBigoRewardVideo;
import com.trivia.fortune.sdk.ad.TrFVideoRewardCallback;
import com.trivia.fortune.sdk.ad.TrFVideoIAP;
import com.trivia.fortune.sdk.bridge.TrFCocosBridge;
import com.trivia.fortune.sdk.ad.TrFVideoManger;
import com.trivia.fortune.sdk.ad.TrFMaxReward;

import org.json.JSONObject;

public class TrFAdSdk {

    public static void showBiddingAd(String ad_source) {
        showAd(new TrFVideoRewardCallback() {
            @Override
            public void onAdOver() {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("ad_source",ad_source);
                    TrFCocosBridge.getInstance().XSSdkCallback("ad_w_over", jsonObject.toString());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onAdError() {
                TrFCocosBridge.getInstance().XSSdkCallback("ad_w_error", "{}");
            }
        });
    }

    private static void showAd(TrFVideoRewardCallback callback) {
        TrFVideoIAP service = null;
        boolean isMaxReady =  TrFMaxReward.getInstance().isReady();
        boolean isBigoReady =  TrFBigoRewardVideo.getInstance().isReady();
        if (isMaxReady){
            Log.e("TriviaW","show max reward");
            service= TrFVideoManger.getReward1Instance();
        }else if (isBigoReady){
            Log.e("TriviaW","show bigo reward");
            service= TrFVideoManger.getReward2Instance();
        }

        if (service == null) {
            callback.onAdError();
            return;
        }

        try {
            service.showVideo(TrFCocosBridge.mainGameActivity.get(), new TrFVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    if (callback != null) {
                        callback.onAdOver();
                    }
                }

                @Override
                public void onAdError() {
                    if (callback != null) {
                        callback.onAdError();
                    }
                }
            });
        } catch (Exception e) {
            try {
                callback.onAdError();
            } catch (Exception e1) {
            }
        }

        if (service instanceof TrFMaxReward) {
            TrFBigoRewardVideo.getInstance().loadRewardAd();
        }
    }

}
